﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class User_ProductDetails : System.Web.UI.Page
{
    ShoppingCartBUS cartBUS = new ShoppingCartBUS();

    BouquetBUS bouBUS = new BouquetBUS();
    DataTable productTB;
    DataView prodcutView;
    string pId = "";
    List<Gift> listGift;
    GCatalogBUS gCBUS = new GCatalogBUS();
    DataTable tbGCatalog = new DataTable();
    DataView gCaView;

    GiftBUS giftBUS = new GiftBUS();
    DataTable tbGift = new DataTable();

    DataTable listGiftSC = new DataTable();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadGift();
            listGift = new List<Gift>();
            Session["Gift"] = listGift;
        }
        pId = Request.QueryString["pId"].ToString();

        productTB = giftBUS.returnGiftByID(pId);
        if (productTB.Rows.Count > 0)
        {
            prodcutView = new DataView(productTB);
            ImgProduct.ImageUrl = prodcutView[0]["G_Image"].ToString();
            lblName.Text = prodcutView[0]["G_Name"].ToString();
            lblPrice.Text = prodcutView[0]["G_Price"].ToString();
            lblDetails.Text = prodcutView[0]["G_Details"].ToString();
            lblPriceBouquet.Text = prodcutView[0]["G_Price"].ToString();
            imgBouquet.ImageUrl = prodcutView[0]["G_Image"].ToString();
        }
        else
        {
            productTB = bouBUS.returnAllBouquet();
            prodcutView = new DataView(productTB);
            prodcutView.RowFilter = "B_ID='" + pId + "'";
            ImgProduct.ImageUrl = prodcutView[0]["B_Image"].ToString();
            lblName.Text = prodcutView[0]["B_Name"].ToString();
            lblPrice.Text = prodcutView[0]["B_Price"].ToString();
            lblDetails.Text = prodcutView[0]["B_Details"].ToString();
            lblPriceBouquet.Text = prodcutView[0]["B_Price"].ToString();
            imgBouquet.ImageUrl = prodcutView[0]["B_Image"].ToString();

        }
        
    }

    private void LoadGift()
    {
        tbGCatalog = gCBUS.returnAllGCatalog();
        gCaView = new DataView(tbGCatalog);
        gCaView.RowFilter = "CG_Status=1";
        dlGiftMenu.DataSource = gCaView;
        dlGiftMenu.DataBind();

        dlGift.DataSource = giftBUS.returnGiftByCID(Int32.Parse(gCaView[0]["CG_ID"].ToString()));
        dlGift.DataBind();
    }

    protected void lbnBuy_Click(object sender, EventArgs e)
    {
        cartBUS.Add(pId, 1);
        List<Gift> ls = (List<Gift>)Session["Gift"];
        giftBUS.AddCart(ls);
        Response.Redirect("ShoppingCart.aspx");

    }
    protected void dlGiftMenu_ItemCommand(object source, DataListCommandEventArgs e)
    {
        tbGift = giftBUS.returnGiftByCID(Int32.Parse(e.CommandArgument.ToString()));
        dlGift.DataSource = tbGift;
        dlGift.DataBind();

    }
    protected void dlGift_ItemCommand(object source, DataListCommandEventArgs e)
    {
        listGiftSC = giftBUS.returnGiftByID(e.CommandArgument.ToString());

        Gift item = new Gift(listGiftSC.Rows[0]["G_ID"].ToString(), listGiftSC.Rows[0]["G_Name"].ToString(), float.Parse(listGiftSC.Rows[0]["G_Price"].ToString()), listGiftSC.Rows[0]["G_Details"].ToString(), listGiftSC.Rows[0]["G_Image"].ToString(), Int32.Parse(listGiftSC.Rows[0]["CG_ID"].ToString()), Int32.Parse(listGiftSC.Rows[0]["G_Status"].ToString()));
        List<Gift> ls = (List<Gift>)Session["Gift"];
        ls.Add(item);
        Session["Gift"] = ls;
        dlListShopCart.DataSource = ls;
        dlListShopCart.DataBind();


    }
    protected void dlListShopCart_ItemCommand(object source, DataListCommandEventArgs e)
    {
        List<Gift> ls = (List<Gift>)Session["Gift"];
        foreach (Gift gift in ls)
        {
            if (gift.Id.Equals(e.CommandArgument.ToString()))
            {
                ls.Remove(gift);
                break;
            }
        }
        Session["Gift"] = ls;
        dlListShopCart.DataSource = ls;
        dlListShopCart.DataBind();

    }
}